import Vue from 'vue'
import { sessionStore } from 'store/dist/store.legacy'
const state = {
  list: [1, 2],
  page: 0,
  url: process.env.VUE_APP_prayerList_url,
}

const mutations = {
  setList (state, param) {
    state.page = param.page
    state.list = state.list.concat(param.data)
    sessionStore.set('prayerPage', param.page)
    sessionStore.set('prayerList', state.list)
  }
}

const actions = {
  async changeList ({ state, commit }, param) {
    let page = state.page
    if (page == 0) {
      page = sessionStore.get('prayerPage') || 0
    }
    return await window.axios.post(state.url, { page: ++page }).then(ret => {
      if (ret.data.length > 0) {
        commit('setList', { data: ret.data, page: page })
        return 1
      } else {
        return 0
      }
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}